<!-- 我正在开发 我是刘瑶瑶 QQ：1778851996 昵称：喑 -->
<!-- 这是添加消毒地址 -->
<template>
	<view>
		<!-- 显示选择的地址 -->
		<view class="detail-area">
			<view class="word">详细地址</view>
			<!-- 地址 -->
			<view class="detail">
				<input type="text" :value="locationAddress" name="address" @input="addressInput"/>
			</view>
		</view>
		<!-- 登记 -->
		<view class="sub-btn" @click="addDistinct">登记</view>
	</view>
</template>

<script>
	import app from '@/common/app.js';
	export default {
		data() {
			return {
				id: '',
				// 地址
				locationAddress: '',
				time: '',
				
			}
		},
		onLoad(option) {
			if (option) {
				this.id = option._id
				this.locationAddress = option.locationAddress
			}
		},
		methods: {
			// 选择消毒位置
			chooseLocation: function () {
				uni.chooseLocation({
					success: (res) => {
						console.log(res)
						this.locationAddress = res.address + ' ' + res.name
					}
				})
			},
			// 添加地址
			addDistinct() {
				if (this.locationAddress != undefined) {
					if (this.id != undefined) {
						app.callFunction('update_location',
							{
								"_id": this.id,
								"locationAddress": this.locationAddress
							}, (data, e) => {
								console.log(e)
								if (e.code == '0000') {
									uni.hideLoading()
									uni.showToast({
										title: '重置成功',
										icon: 'none'
									})
									uni.switchTab({
										url: '/pages/location/list/list'
									})
								}
							})
					} else {
						app.callFunction('select_location',
							{
								"locationAddress": this.locationAddress
							}, (data, e) => {
								console.log(data)
								if (data.total >= 1) {
									uni.showToast({
										title: '此位置已经存在',
										icon: 'none'
									})
								} else {
									this.nowTime()
									app.callFunction('add_location',
										{
											"locationAddress": this.locationAddress,
											"time": this.time
										}, (data, e) => {
										console.log(e)
										uni.hideLoading()
										if (e.code == '0000') {
											uni.showToast({
												title: '添加成功',
												icon: 'none'
											})
											uni.switchTab({
												url: '/pages/location/list/list'
											})
										}
									})
								}
							})
					}
				} else {
					uni.showToast({
						title: '未填写消毒位置',
						icon: 'none'
					})
				}
			},
			// 获取当前时间
			nowTime() {
				let time = new Date();
				let m = time.getMonth() + 1;
				let d = time.getDate();
				let hh = time.getHours();
				let mm = time.getMinutes();
				let ss = time.getSeconds();
				this.time =
					time.getFullYear() + '-' + (m > 9 ? m : '0' + m) + '-' + (d > 9 ? d : '0' + d) + ' ' + (hh > 9 ? hh : '0' + hh) + ':' + (mm > 9 ? mm : '0' + mm);
				console.log(this.time)
			},
			// 双向绑定表单
			addressInput(e) {
				this.locationAddress = e.target.value
			}
		}
	}
</script>

<style scoped>
	@import '../../../common/iconfont.css';
	/* 选择消毒位置与详细位置 */
	.detail-area {
		display: flex;
		align-items: center;
		width: 750rpx;
		box-sizing: border-box;
		padding: 40rpx;
	}
	/* 左边文字 */
	.word {
		margin-right: 40rpx;
		font-size: 36rpx;
		color: #333;
	}
	/* 详细地址 */
	.detail {
		width: 454rpx;
		height: 100rpx;
		display: flex;
		align-items: center;
		padding-left: 30rpx;
		line-height: 80rpx;
		font-size: 32rpx;
		color: #333;
		background-color: rgba(0, 0, 0, 0.01);
	}
	/* 提交按钮 */
	.sub-btn {
		margin: 50rpx 220rpx;
		width: 300rpx;
		height: 110rpx;
		border: 5rpx solid #fff;
		border-radius: 60rpx;
		box-shadow:0px 8px 12px rgba(0,0,0,0.25);
		background-color: #6DDDCB;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 40rpx;
		color: #fff;
	}
</style>
